// d:\douyin_mini_project\pro-1\pages\preview\preview.js
import {list} from '../../videos/images'
Page({
  data: {
    list:list,
    imgIndex:0
  },
  onLoad() {
    
  },
  onShow(){
    this.createRadomView()
  },
  createRadomView(){
    return 
    let list = []
    for(let i = 0;i < 20;i++){
      let height = 300  + Math.round(Math.random() * 100)
      list.push({
        height
      })
    }
    this.setData({
      list:list
    })
  },
  chooseImg(e){
    let index = e.currentTarget.dataset.index
    this.setData({
      imgIndex:index
    })
  },
  authorizeUser(){
    let _this = this
    tt.getSetting({
      success(res) {
        console.log(res);
        if(!res.authSetting['scope.album']){
          return tt.authorize({
            scope:'scope.album',
            success(){
              tt.showToast({
                title:'授权成功',
                icon:"success"
              })
            },
            fail(){
              tt.showToast({
                title:'授权失败',
                icon:"fail"
              })
            },
            complete(){}
          })
        }
        _this.openADMask()
      },
      fail(err) {
        console.log("getSetting 调用失败", err);
      },
    });
    
  },
  openADMask(){
    console.log('授权成功，打开下载mask')
    this.setData({
      mask1:true
    })
    
  },
  closeMask(e){
    this.setData({
      mask1:false
    })
  },
  voidFun(){
    
  },
  downloadImg(){
    let _this = this
    //console.log(this.data.list[this.data.imgIndex].imgUrl)
    tt.showLoading({
      title:'下载中'
    })
    tt.downloadFile({
      url:this.data.list[this.data.imgIndex].imgUrl,
      success(res){
        console.log(res)
        let path = res.tempFilePath
        tt.saveImageToPhotosAlbum({
          filePath:path,
          success(){
            tt.hideLoading()
            tt.showToast({
              title:'已保存到相册',
              icon:'success'
            })
            _this.setData({
              mask1:false
            })
          }
        })
        
      },
      fail(err){}
    })
  }
})